package com.solidumgame.martillo.tablewood
{
	import com.greensock.TweenMax;
	import com.pblabs.engine.PBE;
	import com.pblabs.engine.entity.IEntity;
	import com.solidumgame.martillo.tablewood.events.TableWoodGenericEvent;
	
	import flash.events.EventDispatcher;
	import flash.geom.Point;

	public class TableWood extends EventDispatcher
	{

		public static const TYPE:String = "TableWood";
		
		private var spatialComp:TableWoodSpatialComponent;
		private var upRenderer:TableWoodUpRendererComponent;
		private var renderer:TableWoodRendererComponent;
		private var logic:TableWoodLogicComponent;
		private var sound:TableWoodSoundComponent;
		private var myEntity:IEntity;
		
		public function TableWood()
		{
			spatialComp = new TableWoodSpatialComponent();
			upRenderer = new TableWoodUpRendererComponent();
			renderer = new TableWoodRendererComponent();
			logic = new TableWoodLogicComponent();
			sound = new TableWoodSoundComponent();
			
			
			// Allocate new entity and add components.
			myEntity = PBE.allocateEntity();
			myEntity.addComponent(spatialComp, "Spatial");
			myEntity.addComponent(upRenderer,"UpRenderer");
			myEntity.addComponent(renderer, "Renderer");
			myEntity.addComponent(logic, "Logic");
			myEntity.addComponent(sound, "Sound");
			myEntity.initialize("TableWood");
			
			myEntity.eventDispatcher.addEventListener(TableWoodGenericEvent.TABLEWOOD_EXIT_EVENT, onTableWoodExit);
			myEntity.eventDispatcher.addEventListener(TableWoodGenericEvent.TABLEWOOD_ENTER_EVENT, onTableWoodEnter);
			
			logic.throwInTableWood();
		}
		
		
		protected function onTableWoodExit(e:TableWoodGenericEvent):void{
			dispatchEvent(new TableWoodGenericEvent(TableWoodGenericEvent.TABLEWOOD_EXIT_EVENT));
		}
		
		
		protected function onTableWoodEnter(e:TableWoodGenericEvent):void{
			dispatchEvent(new TableWoodGenericEvent(TableWoodGenericEvent.TABLEWOOD_ENTER_EVENT));
		}
		
		public function throwOutTableWood():void{
			logic.throwOutTableWood();
		}

		public function remove():void{
			myEntity.destroy();
			
			sound = null;
			myEntity = null;
			spatialComp = null;
			logic = null;
			renderer = null;
		}
		
	}
}